<template>
    <div class="block">
        <el-row class="block-item">
            <exRadioGroup label="类型" v-model="type" :dictList="typeList"  size="mini"></exRadioGroup>
            <exSwitch label="仅保留首次创新高" v-model="onlyFirst" class="mt5"></exSwitch>
            <el-button type="primary" @click="query" size="mini" style="height:28px;margin-top:4px">查询</el-button>
        </el-row>
        <exPubTable ref="newTable" :cfg.sync="newcfg" :exData="newList" @getDataList="queryNewList">
            <exColumn label="名称" width="100">
                <template slot-scope="s">
                    <klinePic :code="s.row.c" :name="s.row.n"></klinePic>
                </template>
            </exColumn>
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import klinePic from "../trade/comp/klinePic"

export default {
    components:{
        klinePic,
    },
    data() {
        var that = this;
        return {
            isLoad: false,
            type : "cy",
            typeList:[{"name":"所有",value:""},{"name":"创业",value:"cy"},{"name":"深证",value:"sz"},{"name":"上证",value:"sh"}],
            onlyFirst : true,
            newList:[],
            newcfg:{
                usePage:false,
                showIndex: true,
                btns:[
                    {name:"比对",handle:({row})=>{
                        this.addComp(row);
                    }},
                    // {name:"添加记录",handle:({row})=>{
                    //     this.addRecord(row);
                    // }}
                ],
                columns2: [
                    {prop:"c",label:"代码",width:80,sortable:true},
                    // {prop:"f14",label:"名称",width:100,sortable:true},
                    {prop:"tm",label:"时间",width:80,sortable:true,align:"right",formatter(row,col,val,idx){
                        val = ""+val;
                        if(val.length==5){
                            return "0"+val.substring(0,1)+":"+val.substring(1,3)+":"+val.substring(3,5)
                        }else{
                            return val.substring(0,2)+":"+val.substring(2,4)+":"+val.substring(4,6)
                        }
                        // return Math.floor(val/10000)+":"+ (Math.floor(val/100)%100+":"+(val%100));
                    }},
                    {prop:"i",label:"价格",width:180,sortable:true,align:"right",getColor(s){return "red"},formatter(row,col,val,idx){
                        return val*1;
                    }},

                ],
            }
        }
    },
    computed:{
    },
    provide () {
      return {
      }
    },

    mounted() {

    },

    methods: {
        addComp(item){
            window.$vue.addCompItem({code:item.c,name:item.n});
        },
        query(){
            if(this.$refs.newTable) this.$refs.newTable.reQuery();
        },
        queryNewList(){
            this.$axios.get(`http://push2ex.eastmoney.com/getAllStockChanges?type=8213&pageindex=0&pagesize=164&ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wzchanges`)
            .then((res) => {
                if (res.data.data) {
                    this.newList = res.data.data.allstock;

                    if(this.type){
                        this.newList = this.newList.filter(item=>{
                            var first = item.c.substring(0,1);
                            if(this.type=="cy" && first!="3") return false;
                            else if(this.type=="sh" && first!="6") return false;
                            else if(this.type=="sz" && first!="0") return false;

                            return true;
                        })
                    }

                    if(this.onlyFirst){
                        this.getKLineInfo(0)
                    }

                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        getKLineInfo(idx){
            if(idx>=this.newList.length) return;
            var item = this.newList[idx];
            var code = item.c;
            // if(code!="300898") return;
            var first = code.substring(0,1);
            if(code=="000001"){
                code= "1."+code;
            }else if(first=="0" || first=="3" || first=="1"){
                code = "0."+code;
            }else {
                code = "1."+code;
            }
            this.$axios.get(`http://push2his.eastmoney.com/api/qt/stock/kline/get?secid=${code}&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&ut=7eea3edcaed734bea9cbfc24409ed989&klt=101&fqt=1&beg=0&end=20500000`)
            .then((res) => {
                if (res.data.data) {
                    var data = res.data.data;
                    var len = data.klines.length;
                    var list = data.klines;
                    if(len>60){
                        list = list.splice(len-60,60)
                    }
                    len = list.length;
                    var secondDay =list[len-3]
                    if(!secondDay) return;
                    var highest = secondDay.split(",")[3]*1;
                    for(var i=len-4;i>=0;i--){
                        var dayInfo = list[i];
                        var arr = dayInfo.split(",");
                        var dayHighest = arr[3]*1
                        if(dayHighest>highest){ //存在比前天高的,则为首次突破
                            this.getKLineInfo(idx+1)
                            return;
                        }
                    }
                    console.info("remove:"+item.c+item.n)
                    this.newList.splice(idx,1);
                    this.getKLineInfo(idx)
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
    }
    
}
</script>
